﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolutionModel;
using SFast;
using System.Transactions;
using NLite.Data;
//using EntityFramework.Extensions;

namespace SolutionDAL
{
    public class T_MenuInRoleDAL
    {

        #region 通过角色ID获取该角色的所有菜单
        /// <summary>
        /// 通过角色ID获取该角色的所有菜单
        /// </summary>
        /// <param name="roleid">角色ID</param>
        /// <returns></returns>
        public List<T_MenuInRole> GetMuneInRoleID(String roleid)
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            return db.T_MenuInRoles.Where(o => o.RoleID == roleid).ToList();

        }
        #endregion

        

        #region 添加或编辑角色的菜单权限
        /// <summary>
        /// 添加或编辑角色的菜单权限
        /// </summary>
        /// <param name="roleId">角色 ID</param>
        /// <param name="menuIds">新菜单ID集合</param>
        public void InsertOrUpdate(String roleId, List<int> menuIds)
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            db.UsingTransaction(() =>
            {
                try
                {
                    //删除当前角色的所有菜单权限
                    db.T_MenuInRoles.Delete(o => o.RoleID == roleId);

                    if (menuIds.Count > 0)
                    {
                        T_MenuInRole m = null;
                        foreach (int id in menuIds)
                        {
                            m = new T_MenuInRole();
                            m.RoleID = roleId;
                            m.MenuID = id;
                            db.T_MenuInRoles.Insert(m);
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
            });
        }
        #endregion

        /// <summary>
        /// 为某个角色添加某菜单权限，此方法为了实现添加新菜单时直接给超级管理员添加上权限
        /// </summary>
        /// <param name="roleID">角色ID</param>
        /// <param name="menuID">菜单ID</param>
        public void Add(String roleID, int menuID)
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            T_MenuInRole m = new T_MenuInRole();
            m.RoleID = roleID;
            m.MenuID = menuID;
            db.T_MenuInRoles.Insert(m);

        }
    }
}
